layui.use(['form', 'layer', 'upload'], function () {
    const form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        upload = layui.upload,
        $ = layui.jquery;

    //封面图上传
    let coverUrl = null;
    const uploadInst = upload.render({
        elem: '#test1'
        , url: host + '/a03/umsmember/uploadImage'
        , method: 'post'  //可选项。HTTP类型，默认post
        , field: 'multipartFile'
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        }
        , done: function (res) {
            //如果上传失败
            if (res.code !== 200) {
                return layer.msg('上传失败');
            } else {
                //上传成功
                coverUrl = res.data;
            }
        }
        , error: function () {
            //演示失败状态，并实现重传
            const demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
    });

    // //点击取消
    // $("body").on("click", "#cancelNews", function () {
    //     layer.closeAll("iframe");
    // });

    form.on("submit(addNews)", function (data) {
        var data1 = form.val("formTest");
        // console.log(data1);
        //do something
        $.ajax({
            url: $.cookie("tempUrl") + "SysDoctorCase/insert",
            type: "POST",
            datatype: "application/json",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({
                doctorId: $.cookie("doctorId"),
                menberName: data1.menberName,
                menberSex: data1.menberSex,
                menberAge: data1.menberAge,
                disease: data1.disease,
                cbzd: data1.cbzd,
                bqms: data1.bqms,
                lczd: data1.lczd,
                jyjl: data1.jyjl,
                jcbw: data1.jcbw,
                blzl: coverUrl,
                zdyj: data1.zdyj,
                zljy: data1.zljy,
            }),
            success: function (result) {
                if (result.code === 200) {
                    layer.msg("新增成功");
                    setTimeout(function () {
                        // top.layer.close(index);
                        // top.layer.msg("新增成功2");
                        layer.closeAll("iframe");
                        //刷新父页面
                        parent.location.reload();
                    }, 500);
                } else {
                    layer.msg("请求失败", {icon: 7, anim: 6});
                }
            }
        });

        return false;
    });
});